

clear
gen t = .

tempfile dat
save `dat'

* Iterate along calendar number around launch
forvalues t = 1/60{

	* Month-by-month
	use "${root}\Data\etf_month_data", clear
	keep if t == `t'

	* EW
	egen ret_e = mean(ret), by(q date)

	* VW
	forvalues k = 0/1{
		bysort date: asgen temp_ret_`k' = (ret/(q == `k')), w(mktcap_lag)
	}
	gen ret_w = .
	forvalues k = 0/1{
		replace ret_w = temp_ret_`k' if q == `k'
	}
	drop temp_ret*

	* Collapse
	collapse ret_e ret_w rf mktrf smb hml rmw cma umd me ia roe, by(q date)

	* Excess returns
	gen exret_e = ret_e - rf
	gen exret_w = ret_w - rf

	* % scale
	foreach var of varlist mktrf-exret_w{
		replace `var' = `var' * 100
	}
	
	gen t = `t'
	order q t date 

	append using `dat'
	save `dat', replace

	
}




clear
gen q = .
tempfile alphas
save `alphas'

forvalues q = 0/1{

	use `dat' if q == `q', clear

	forvalues t = 1/60{
		
		gen mktrf_`t' = mktrf * (t == `t') 
		
	}	

	* Seemingly Unrelated Regressions
	reg exret_w bn.t i.t#c.mktrf i.t#c.smb i.t#c.hml i.t#c.umd, hascons cluster(date)

	matrix coef_all = e(b)
	matrix covar_all = e(V)

	matrix coef = coef_all[1,1..60]
	matrix covar = covar_all[1..60,1..60]

	forvalues t = 1/60{
		
		* Coefficients and standard errors
		matrix ones_sub = J(1,`t',1)
		matrix coef_sub = coef[1,1..`t']
		matrix covar_sub = covar[1..`t',1..`t']
		matrix alpha_ffc4_sub = ones_sub * coef_sub'
		matrix se_ffc4_sub = ones_sub * covar_sub * ones_sub'
		local alpha_ffc4_sub = alpha_ffc4_sub[1,1]
		local se_ffc4_sub = se_ffc4_sub[1,1]
		
		clear
		set obs 1
		gen q = `q'
		gen t = `t'
		gen alpha_ffc4 = `alpha_ffc4_sub'
		gen se_ffc4 = sqrt(`se_ffc4_sub')

		append using `alphas'
		save `alphas', replace
		
	}
}

use `alphas', clear

gen CAR = alpha_ffc

gen upper = CAR + 1.96*se_ffc4
gen lower = CAR - 1.96*se_ffc4


twoway (line CAR t if q == 0, lc(b) lp(shortdash) lw(medthick) leg(label(1 "Broad-Based ETFs") cols(1) ring(0) position(8) bmargin(0.5) region(lwidth(none) fc(none)))) ///
	   (line CAR t if q == 1, lc(b) lp(solid) lw(medthick) leg(label(2 "Specialized ETFs"))) ///
       (line u t if q == 0, lc(b) lp(dot) lw(medthick) leg(label(3 ""))) ///
       (line l t if q == 0, lc(b) lp(dot) lw(medthick) leg(label(4 ""))) ///
       (line u t if q == 1, lc(b) lp(dot) lw(medthick) leg(label(5 ""))) ///
       (line l t if q == 1, lc(b) lp(dot) lw(medthick) leg(label(6 "")) ///
	   xtitle("Trading Months Relative to Launch Date") ytitle("CAR FFC4 (%)") /// 
	   xlab(0(12)60)) ///
   	
*




